<template>
  <el-table
    :data="user.filter(data => !search || data.name.toLowerCase().includes(search.toLowerCase()))"
    style="width: 100%"
  >
    <el-table-column type="selection" width="55"></el-table-column>
    <el-table-column label="账号" prop="username"></el-table-column>
    <el-table-column label="昵称" prop="name"></el-table-column>
    <el-table-column align="right">
      <template slot="header" slot-scope="scope">
        <el-input v-model="search" size="mini" placeholder="输入关键字搜索" />
      </template>
      <template slot-scope="scope">
        <el-button size="mini" @click="edit(scope.row.id)">编辑</el-button>
        <el-button size="mini" type="danger" @click="open(scope.row.id)">删除</el-button>

        <!-- <el-button size="mini" type="danger" @click="remove(scope.row.id)">删除</el-button> -->
      </template>
    </el-table-column>
  </el-table>
</template>

<script>
export default {
  data() {
    return {
      user: [],
      search: ""
    };
  },

  methods: {
    open(id) {
      this.$confirm("此操作将永久删除该文件, 是否继续?", "提示", {
        confirmButtonText: "确定",
        cancelButtonText: "取消",
        type: "warning"
      })
        .then(() => {
          this.remove(id);
        })
        .catch(() => {
          this.$message({
            type: "info",
            message: "已取消删除"
          });
        });
    },
    fetch() {
      this.$http.get("/v1.Admin/select").then(res => {
        this.user = res.data.data;
      });
    },

    edit(id) {
      this.$router.push("/user/edit/" + id);
    },

    remove(id) {
      this.$http.get("/v1.Admin/delete/" + id).then(res => {
        if (res.data.data == true) {
          this.$message({
            message: "删除成功",
            type: "success"
          });
          this.fetch();
        } else {
          this.$notify.error({
            title: "失败",
            message: res.data.data
          });
        }
      });
    }
  },
  created() {
    this.fetch();
  }
};
</script>